package crypt;

import java.io.DataInputStream;
import java.security.*;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import org.apache.commons.codec.binary.Base64;

/**
 * Class that handles verification of signatures
 * 
 * @author jonas.lindmark
 */
public class VerifyHandler {
    public byte[] body;
    public byte[] sig;
    /**
     * Verfies the signature
     * 
     * @param data Message that was signed
     * @param pubfile Public keyfile
     * @param signString The signature
     * @return False if message is corrupt, true otherwise.
     */
    public boolean verifyByString(byte[] data, String pubfile, String signString)
    {
        body = data;
        try {
            PublicKey pk = null;
            byte[] sign = Base64.decodeBase64(signString);
            sig = sign;
            
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(pubfile));
            pk = (PublicKey)in.readObject();
            in.close();
            
            Signature signer = Signature.getInstance("SHA1withDSA");
            signer.initVerify(pk);

            signer.update(data);

            if (signer.verify(sign))
                return true;
            else
                return false;

        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
            return false;
        }
    }
}
